package com.situ.day3;

import org.junit.Test;

public class ArrayDemo {
    @Test
    public void test1() {
        int score1 = 34;
        int score2 = 54;
        int score3 = 98;
        int score4 = 58;
        int sum = score1 + score2 + score3 + score4;

        int[] array = new int[4];
        array[0] = 23;
        array[1] = 3;
        array[2] = 4;
        array[3] = 24;
        //length:属性 数组长度
        System.out.println(array.length);//4
        for (int i = 0; i < array.length; i++) {
            //System.out.println(i);
            System.out.println(array[i]);
        }
        System.out.println("--------");
        for (int i = array.length - 1; i >= 0; i--) {
            System.out.println(array[i]);
        }

        System.out.println("---------");
        for (int i : array) {
            System.out.println(i);
        }
    }

    @Test
    public void test4() {
        int[] array = new int[4];
        array[0] = 23;
        array[1] = 3;
        array[2] = 4;
        array[3] = 24;
        int sum = 0;
        for (int i = 0; i < array.length; i++) {
            sum = sum + array[i];
        }
        System.out.println("sum: " + sum);
    }

    @Test
    public void test6() {
        /*int[] array = new int[4];
        array[0] = 23;
        array[1] = 3;
        array[2] = 4;
        array[3] = 24;*/

        int[] array = {23, 33, 4, 64};
        //假设第一个是最大的
        int max = array[0];
        for (int i = 1; i < array.length; i++) {
            if (array[i] > max) {
                max = array[i];
            }
        }
        System.out.println("max: " + max);

        int[] array1 = {3, 43, 64, 6, 76};
        int max1 = getMax(array1);
        System.out.println("max1: " + max1);

        int[] array2 = {56, 64, 34, 79, 68, 13};
        int max2 = getMax(array2);
        System.out.println("max2: " + max2);
    }

    /**
     * 传递任何一个整数类型数组，返回数组的最大值
     * @param array
     * @return
     */
    public int getMax(int[] array) {
        int max = array[0];
        for (int i = 1; i < array.length; i++) {
            if (array[i] > max) {
                max = array[i];
            }
        }
        return max;
    }

    @Test
    public void test36() {
        int[] array = {23, 33, 4, 64};
        int min = array[0];
        for (int i = 1; i < array.length; i++) {
            if (array[i] < min) {
                min = array[i];
            }
        }
        System.out.println("min: " + min);
    }

    @Test
    public void test16() {
        int[] array = new int[4];
        array[0] = 23;
        array[1] = 3;
        array[2] = 4;
        array[3] = 24;
        // array.fori
        for (int i = 0; i <= array.length; i++) {
            //array[4]
            //java.lang.ArrayIndexOutOfBoundsException: Index 4 out of bounds for length 4
            System.out.println(array[i]);
        }
    }

    @Test
    public void test55() {
        int[] array = {40, 17, 21, 1};
        for (int i = 1; i <= array.length - 1; i++) {
            for (int j = 0; j < array.length - i; j++) {
                if (array[j] > array[j + 1]) {
                    int temp = array[j];
                    array[j] = array[j + 1];
                    array[j + 1] = temp;
                }
            }
        }

        for (int i = 0; i < array.length; i++) {
            System.out.println(array[i]);
        }
    }
}
